package cn.riverluoo.contralpanel;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.xml.parsers.ParserConfigurationException;

import cn.riverluoo.editor.TextEditor;
import cn.riverluoo.effectiveness.ShowDTD;
import cn.riverluoo.effectiveness.XsdValidataXML;
import cn.riverluoo.fileoperation.FileOperation;
import cn.riverluoo.font.SetFont;
import cn.riverluoo.normative.Normative;
import cn.riverluoo.parse.DomParse;
import cn.riverluoo.parse.ShowSax;
import cn.riverluoo.run.Run;

public class ContralPanel extends JPanel implements ActionListener{

	TextEditor textEditor;

	public FileOperation fileOperation;

	JMenuBar menubar;
	public ContralPanel(TextEditor textEditor)
	{
		this.textEditor=textEditor;
		fileOperation=new FileOperation(textEditor);
		creatMenu();
	}

	public void creatMenu()
	{
		menubar=new JMenuBar();
		JMenu file=new JMenu("文件");
		JMenu style=new JMenu("格式");
		JMenu parse=new JMenu("解析");
		JMenu yanzheng=new JMenu("验证");
		JMenu about=new JMenu("帮助");
		JMenu run=new JMenu("运行");

		menubar.add(file);
		menubar.add(style);
		menubar.add(parse);
		menubar.add(yanzheng);
		menubar.add(run);
		menubar.add(about);

		JMenuItem newFile=new JMenuItem("新建");
		setShortcut(newFile, KeyEvent.VK_N);
		JMenuItem open=new JMenuItem("打开");
		setShortcut(open, KeyEvent.VK_O);
		JMenuItem save=new JMenuItem("保存");
		setShortcut(save, KeyEvent.VK_S);
		JMenuItem quit=new JMenuItem("关闭");
		setShortcut(quit, KeyEvent.VK_Q);

		JMenuItem setFont=new JMenuItem("字体设置");

		JMenuItem dom01=new JMenuItem("dom解析");
		JMenuItem sax01=new JMenuItem("sax解析");

		JMenuItem guifan=new JMenuItem("规范性验证");
		JMenuItem youxiaoDTD=new JMenuItem("DTD有效性验证");
		JMenuItem youxiaoXSD=new JMenuItem("XSD有效性验证");


		JMenuItem about1=new JMenuItem("相关信息");
		JMenuItem about2=new JMenuItem("使用说明");

		JMenuItem javaRun=new JMenuItem("编译运行");
		setShortcut(javaRun, KeyEvent.VK_F11);
		JMenuItem htmlRun=new JMenuItem("浏览器运行");
		setShortcut(htmlRun, KeyEvent.VK_F12);

		file.add(newFile);
		file.addSeparator();
		file.add(open);
		file.addSeparator();
		file.add(save);
		file.addSeparator();
		file.add(quit);

		style.add(setFont);

		parse.add(dom01);
		parse.addSeparator();
		parse.add(sax01);
		parse.addSeparator();

		yanzheng.add(guifan);
		yanzheng.addSeparator();
		yanzheng.add(youxiaoDTD);
		yanzheng.addSeparator();
		yanzheng.add(youxiaoXSD);

		about.add(about1);
		about.addSeparator();
		about.add(about2);

		run.add(javaRun);
		run.add(htmlRun);

		newFile.addActionListener(this);
		open.addActionListener(this);
		save.addActionListener(this);
		quit.addActionListener(this);

		setFont.addActionListener(this);

		dom01.addActionListener(this);
		sax01.addActionListener(this);

		guifan.addActionListener(this);
		youxiaoDTD.addActionListener(this);
		youxiaoXSD.addActionListener(this);

		about1.addActionListener(this);
		about2.addActionListener(this);

		javaRun.addActionListener(this);
		htmlRun.addActionListener(this);
	}

	public void addMenuBar()
	{
		textEditor.setJMenuBar(menubar);
	}

	public void setShortcut(JMenuItem jmi,int key)
	{
		KeyStroke ms1 = KeyStroke.getKeyStroke(key,InputEvent.CTRL_MASK);
		jmi.setMnemonic(key);
		jmi.setAccelerator(ms1);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("保存"))
		{
			if(fileOperation.save())
				textEditor.setLeftSplitPane(fileOperation.setFileList());
		}
		if(e.getActionCommand().equals("新建"))
		{
			if(fileOperation.creat())
				textEditor.setLeftSplitPane(fileOperation.setFileList());
		}
		if(e.getActionCommand().equals("打开"))
		{
			if(fileOperation.open())
				textEditor.setLeftSplitPane(fileOperation.setFileList());
		}
		if(e.getActionCommand().equals("关闭"))
		{
			textEditor.dispose();
		}
		if(e.getActionCommand().equals("字体设置"))
		{
			new SetFont(textEditor);
		}
		if(e.getActionCommand().equals("dom解析"))
		{
			DomParse dp=new DomParse();

			String s=".xml";
			fileOperation.save();
			String src=fileOperation.getPath();

			String newsrc=src.replaceAll("\\\\","\\\\\\\\");
			String name=fileOperation.getFileName();
			String newname=newsrc+String.valueOf(name)+s;

			dp.parse(newname);

		}
		if(e.getActionCommand().equals("sax解析"))
		{
			ShowSax ss=new ShowSax();

			String s=".xml";
			fileOperation.save();
			String src=fileOperation.getPath();

			String newsrc=src.replaceAll("\\\\","\\\\\\\\");
			String name=fileOperation.getFileName();
			String newname=newsrc+String.valueOf(name)+s;

			try {
				ss.show(newname);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}


		if(e.getActionCommand().equals("规范性验证")){

			Normative nor=new Normative();

			String s=".xml";
			fileOperation.save();
			String src=fileOperation.getPath();

			String newsrc=src.replaceAll("\\\\","\\\\\\\\");
			String name=fileOperation.getFileName();
			String newname=newsrc+String.valueOf(name)+s;

			nor.showNormative(newname);

		}
		if(e.getActionCommand().equals("DTD有效性验证")){
			ShowDTD sd=new ShowDTD();

			String s=".xml";
			fileOperation.save();
			String src=fileOperation.getPath();

			String newsrc=src.replaceAll("\\\\","\\\\\\\\");
			String name=fileOperation.getFileName();
			String newname=newsrc+String.valueOf(name)+s;

			try {
				sd.showDTDMessage(newname);
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
		if(e.getActionCommand().equals("XSD有效性验证")){
			XsdValidataXML xv=new XsdValidataXML();

			String s=".xml";
			String x=".xsd";
			fileOperation.save();
			String src=fileOperation.getPath();

			String newsrc=src.replaceAll("\\\\","\\\\\\\\");
			String name=fileOperation.getFileName();
			String newname=newsrc+String.valueOf(name)+s;
			String newxsd=newsrc+String.valueOf(name)+x;

			xv.showXsdValidata(newname, newxsd);


		}

		if(e.getActionCommand().equals("相关信息"))
		{
			JOptionPane.showMessageDialog(null,"开发人员：riverluoo.cn\n版本:1.0.0","相关信息",JOptionPane.PLAIN_MESSAGE);
		}
		if(e.getActionCommand().equals("使用说明"))
		{
			JOptionPane.showMessageDialog(null,"1.请确认正确配置JAVA环境,否则无法进行编译运行\n2.xml文件需和dtd文件在同一文件夹"
					+"\n3.xml文件需和xsd文件在同一文件夹\n4.运行xml文档请设置好默认浏览器","使用说明",JOptionPane.PLAIN_MESSAGE);
		}

		if(e.getActionCommand().equals("编译运行"))
		{
			fileOperation.save();
			String src=fileOperation.getPath();
			String name=fileOperation.getFileName();
			new Run().javaRun(src, name);
		}
		if(e.getActionCommand().equals("浏览器运行"))
		{

			fileOperation.save();
			String src=fileOperation.getPath();
			String name=fileOperation.getFileName();

			new Run().htmlRun(src, name);
		}
	}


}
